package com.heima.netty.async;

import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.util.concurrent.DefaultPromise;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutionException;

/**
 * Netty Promise
 * Promise Extends Netty Future
 * @author ZhengDp
 * @Date 2023/6/24 22:15
 */
@Slf4j
public class NettyPromiseTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        EventLoop eventLoop = new NioEventLoopGroup(1).next();


        DefaultPromise<Integer> promise = new DefaultPromise<>(eventLoop);
        new Thread(() -> {
           try {
               log.info("Calculate ...");
               Thread.sleep(1000);
               promise.setSuccess(80);
           } catch (InterruptedException e) {
               throw new RuntimeException(e);
           }
        }).start();

        // 接收结果
        log.info("Waiting  Result ...");
        log.info("Result : {}",promise.get());
    }
}
